IF NOT EXISTS (SELECT * FROM Sys.syscolumns where id = OBJECT_ID('Biz_Order') AND [Name]='OrderEmptyStatus')
BEGIN
	ALTER TABLE dbo.Biz_Order ADD OrderEmptyStatus INT
END
GO


CREATE TYPE [dbo].[OrderDetailTableType] AS TABLE(
	[DetailID] [uniqueidentifier] NOT NULL
)
GO


CREATE PROCEDURE usp_UpdateOrderEmptyStatus
@OrderID uniqueidentifier
,@OrderDetailTableType OrderDetailTableType READONLY
as
UPDATE Biz_Order SET OrderEmptyStatus = 
CASE WHEN EXISTS (
SELECT * FROM Biz_Order_Detail WHERE OrderID=Biz_Order.OrderID AND FollowResult=0
AND DetailID NOT IN (SELECT DetailID FROM @OrderDetailTableType)
) THEN 1 ELSE 2 END
WHERE OrderID=@OrderID
